<?php

require('./configs/include.php');

class c_publicar_categoria extends super_controller {

    public function publicar() {

        if (is_uploaded_file($this->files->imagen['tmp_name'])) {
            $img_type = $this->files->imagen['type'];
            $img_name = date("D jS M Y h i s a e");
            $subcadena = explode(".", $this->files->imagen['name']);
            $type = end($subcadena);
            if (((strpos($img_type, "gif") || strpos($img_type, "jpeg") ||
                    strpos($img_type, "jpg")) || strpos($img_type, "png"))) {
                if (!@move_uploaded_file($this->files->imagen['tmp_name'], C_FULL_PATH . "recursos/" . $img_name . "." . $type)) {
                    throw_exception("No se guardo la imagen");
                }

                $noticia = new noticia();
                $noticia->set('contenido', $this->post->contenido);
                $noticia->set('fecha', date("Y/m/d H:i"));
                $noticia->set('imagen', $this->gvar['l_global'] . "recursos/" . $img_name . "." . $type);
                $noticia->set('titulo', $this->post->titulo);

                $this->orm->connect();
                $this->orm->insert_data("normal", $noticia);
                $this->orm->close();
                
            } else {
                throw_exception("Formato de imagen desconocido");
            }
        } else {
            throw_exception("No se pudo cargar la imagen");
        }

        $this->type_warning = "success";
        $this->msg_warning = "Noticia publicada correctamente";
        $this->temp_aux = 'message.tpl';
        $this->engine->assign('type_warning', $this->type_warning);
        $this->engine->assign('msg_warning', $this->msg_warning);
    }

    public function display() {

        $where = array(array('name' => $this->gvar['n_publicar_noticia'], 'link' => $this->gvar['l_publicar_noticia']));
        $this->engine->assign('title', $this->gvar['n_publicar_noticia']);
        $this->engine->assign('active', $this->gvar['n_publicar_noticia']);
        $this->engine->assign('where', $where);
        $this->engine->display('header2.tpl');
        $this->engine->display($this->temp_aux);
        $this->engine->display('publicar_noticia.tpl');
        $this->engine->display('footer2.tpl');
    }

    public function run() {
        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
            $this->temp_aux = 'message.tpl';
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
        }

        $this->display();
    }

}

$call = new c_publicar_categoria();
$call->run();
?>